package paidaxing.blog.comment.controller;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import paidaxing.blog.comment.dto.CommentDTO;
import paidaxing.blog.comment.dto.request.CommentRequestDTO;
import paidaxing.blog.comment.dto.response.CommentResponseDTO;
import paidaxing.blog.comment.service.CommentService;
import paidaxing.blog.common.result.Result;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comments")
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * 发布评论
     *
     * @param commentRequestDTO 评论请求DTO
     * @return 创建的评论
     */
    @PostMapping
    public Result<CommentDTO> createComment(@RequestBody CommentRequestDTO commentRequestDTO) {
        if (log.isInfoEnabled()) {
            log.info("发布评论: {}", commentRequestDTO);
        }
        // 检查用户是否登录
        StpUtil.checkLogin();
        CommentDTO commentDTO = commentService.createComment(commentRequestDTO);
        if (commentDTO == null) {
            return Result.failed("评论发布失败");
        }
        return Result.success("评论发布成功!", commentDTO);
    }

    /**
     * 获取评论详情
     *
     * @param commentId 评论ID
     * @return 评论详情
     */
    @GetMapping("/{commentId}")
    public Result<CommentDTO> getCommentDetail(@PathVariable Long commentId) {
        if (log.isInfoEnabled()) {
            log.info("获取评论详情，ID: {}", commentId);
        }
        CommentDTO commentDTO = commentService.getCommentById(commentId);
        if (commentDTO == null) {
            return Result.failed("评论不存在");
        }
        return Result.success("获取评论详情成功!", commentDTO);
    }

    /**
     * 更新评论
     *
     * @param commentId 评论ID
     * @param commentRequestDTO 评论请求DTO
     * @return 更新后的评论
     */
    @PutMapping("/{commentId}")
    public Result<CommentDTO> updateComment(@PathVariable Long commentId, @RequestBody CommentRequestDTO commentRequestDTO) {
        if (log.isInfoEnabled()) {
            log.info("更新评论: {}, ID: {}", commentRequestDTO, commentId);
        }
        // 检查用户是否登录
        StpUtil.checkLogin();
        CommentDTO commentDTO = commentService.updateComment(commentId, commentRequestDTO);
        if (commentDTO == null) {
            return Result.failed("评论不存在或无权限更新");
        }
        return Result.success("更新评论成功!", commentDTO);
    }

    /**
     * 删除评论
     *
     * @param commentId 评论ID
     * @return 删除结果
     */
    @DeleteMapping("/{commentId}")
    public Result<String> deleteComment(@PathVariable Long commentId) {
        if (log.isInfoEnabled()) {
            log.info("删除评论，ID: {}", commentId);
        }
        // 检查用户是否登录
        StpUtil.checkLogin();
        boolean result = commentService.deleteComment(commentId);
        if (!result) {
            return Result.failed("评论删除失败或无权限删除");
        }
        return Result.success("评论删除成功!");
    }

    /**
     * 获取评论列表（按文章筛选）
     *
     * @param postId 文章ID
     * @param page 页码，默认为1
     * @param size 每页大小，默认为10
     * @return 评论列表
     */
    @GetMapping
    public Result<List<CommentDTO>> getComments(
            @RequestParam Long postId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        if (log.isInfoEnabled()) {
            log.info("获取评论列表，文章ID: {}, 页码: {}, 每页大小: {}", postId, page, size);
        }
        
        // 参数校验
        if (postId == null) {
            return Result.failed("文章ID不能为空");
        }
        
        List<CommentDTO> comments = commentService.getCommentsByPostId(postId, page, size);
        return Result.success("获取评论列表成功!", comments);
    }

    /**
     * 获取文章评论树
     *
     * @param postId 文章ID
     * @return 评论树
     */
    @GetMapping("/posts/{postId}/comments")
    public Result<List<CommentResponseDTO>> getCommentsTree(@PathVariable Long postId) {
        if (log.isInfoEnabled()) {
            log.info("获取文章评论树，文章ID: {}", postId);
        }
        
        // 参数校验
        if (postId == null) {
            return Result.failed("文章ID不能为空");
        }
        
        List<CommentResponseDTO> commentsTree = commentService.getCommentsTreeByPostId(postId);
        return Result.success("获取评论树成功!", commentsTree);
    }
}